package com.education.ai.controller;

import com.education.ai.entity.AbilityAssessment;
import com.education.ai.service.AbilityAssessmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 能力评估控制器
 */
@RestController
@RequestMapping("/api/assessments")
@CrossOrigin(origins = {"http://localhost:3000", "http://localhost:8080", "http://localhost:8081"}, allowCredentials = "true")
public class AbilityAssessmentController {
    
    @Autowired
    private AbilityAssessmentService assessmentService;
    
    /**
     * 获取所有能力评估
     */
    @GetMapping
    public ResponseEntity<List<AbilityAssessment>> getAllAssessments() {
        return ResponseEntity.ok(assessmentService.getAllAssessments());
    }
    
    /**
     * 根据ID获取能力评估
     */
    @GetMapping("/{id}")
    public ResponseEntity<?> getAssessmentById(@PathVariable Long id) {
        return assessmentService.getAssessmentById(id)
                .map(ResponseEntity::ok)
                .orElse(ResponseEntity.notFound().build());
    }
    
    /**
     * 根据学生ID获取能力评估
     */
    @GetMapping("/student/{studentId}")
    public ResponseEntity<List<AbilityAssessment>> getAssessmentsByStudentId(@PathVariable String studentId) {
        return ResponseEntity.ok(assessmentService.getAssessmentsByStudentId(studentId));
    }
    
    /**
     * 根据学科获取能力评估
     */
    @GetMapping("/subject/{subject}")
    public ResponseEntity<List<AbilityAssessment>> getAssessmentsBySubject(@PathVariable String subject) {
        return ResponseEntity.ok(assessmentService.getAssessmentsBySubject(subject));
    }
    
    /**
     * 根据学生ID和学科获取能力评估
     */
    @GetMapping("/student/{studentId}/subject/{subject}")
    public ResponseEntity<List<AbilityAssessment>> getAssessmentsByStudentIdAndSubject(
            @PathVariable String studentId, @PathVariable String subject) {
        return ResponseEntity.ok(assessmentService.getAssessmentsByStudentIdAndSubject(studentId, subject));
    }
    
    /**
     * 获取学生在某学科的最新评估
     */
    @GetMapping("/student/{studentId}/subject/{subject}/latest")
    public ResponseEntity<?> getLatestAssessment(
            @PathVariable String studentId, @PathVariable String subject) {
        return assessmentService.getLatestAssessment(studentId, subject)
                .map(ResponseEntity::ok)
                .orElse(ResponseEntity.notFound().build());
    }
    
    /**
     * 获取学科能力评估的统计数据
     */
    @GetMapping("/statistics/{subject}")
    public ResponseEntity<Map<String, Double>> getSubjectAssessmentStatistics(@PathVariable String subject) {
        return ResponseEntity.ok(assessmentService.getSubjectAssessmentStatistics(subject));
    }
    
    /**
     * 创建能力评估
     */
    @PostMapping
    public ResponseEntity<AbilityAssessment> createAssessment(@RequestBody AbilityAssessment assessment) {
        AbilityAssessment createdAssessment = assessmentService.saveAssessment(assessment);
        return ResponseEntity.status(HttpStatus.CREATED).body(createdAssessment);
    }
    
    /**
     * 生成学生能力评估（使用AI）
     */
    @PostMapping("/generate")
    public ResponseEntity<AbilityAssessment> generateAssessment(
            @RequestParam String studentId, @RequestParam String subject) {
        AbilityAssessment assessment = assessmentService.generateAssessment(studentId, subject);
        return ResponseEntity.status(HttpStatus.CREATED).body(assessment);
    }
    
    /**
     * 更新能力评估
     */
    @PutMapping("/{id}")
    public ResponseEntity<?> updateAssessment(
            @PathVariable Long id, @RequestBody AbilityAssessment assessment) {
        return assessmentService.getAssessmentById(id)
                .map(existingAssessment -> {
                    assessment.setId(id);
                    return ResponseEntity.ok(assessmentService.saveAssessment(assessment));
                })
                .orElse(ResponseEntity.notFound().build());
    }
    
    /**
     * 删除能力评估
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<?> deleteAssessment(@PathVariable Long id) {
        return assessmentService.getAssessmentById(id)
                .map(assessment -> {
                    assessmentService.deleteAssessment(id);
                    return ResponseEntity.ok().build();
                })
                .orElse(ResponseEntity.notFound().build());
    }
} 